package com.zhangchuchu.neohealth.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhangchuchu.neohealth.common.R;
import com.zhangchuchu.neohealth.entity.DTO.FavoritesDTO;
import com.zhangchuchu.neohealth.entity.Favorites;
import com.zhangchuchu.neohealth.service.FavoritesService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * (Favorites)控制层
 *
 * @author makejava
 * @since 2025-04-16 09:57:15
 */
@RestController
@RequestMapping("/favorites")
public class FavoritesController {
    @Resource
    private FavoritesService favoritesService;

    /**
     * 查询所有数据（不分页）
     */
    @GetMapping("/getList")
    public R<List<Favorites>> selectList() {
        return R.success(favoritesService.list());
    }

    /**
     * 分页查询
     */
    @GetMapping
    public R<Page<Favorites>> selectAll(Page<Favorites> page, Favorites favorites) {
        return R.success(favoritesService.page(page, new QueryWrapper<>(favorites)));
    }

    /**
     * 通过主键查询单条数据
     */
    @GetMapping("{id}")
    public R<Favorites> selectOne(@PathVariable Serializable id) {
        return R.success(favoritesService.getById(id));
    }

    /**
     * 新增数据
     */
    @PostMapping
    public R<Boolean> insert(@RequestBody Favorites favorites) {
        return R.success(favoritesService.save(favorites));
    }

    /**
     * 修改数据
     */
    @PutMapping
    public R<Boolean> update(@RequestBody Favorites favorites) {
        return R.success(favoritesService.updateById(favorites));
    }

    /**
     * 删除数据（批量）
     */
    @DeleteMapping
    public R<Boolean> delete(@RequestParam("idList") List<Integer> idList) {
        return R.success(favoritesService.removeByIds(idList));
    }

    // 查询收藏的文章
    @GetMapping("/articles/{userId}")
    public R<List<FavoritesDTO>> getFavoriteArticles(@PathVariable Integer userId) {
        List<FavoritesDTO> articles = favoritesService.getFavoriteArticles(userId);
        return R.success(articles);
    }

    // 查询收藏的贴文
    @GetMapping("/posts/{userId}")
    public R<List<FavoritesDTO>> getFavoritePosts(@PathVariable Integer userId) {
        List<FavoritesDTO> posts = favoritesService.getFavoritePosts(userId);
        return R.success(posts);
    }
}
